/****************************************************************************
**
** MainGump.cpp
**
** Copyright (C) August 2014 Hotride
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
*****************************************************************************
*/
//---------------------------------------------------------------------------
#include "stdafx.h"
//---------------------------------------------------------------------------
TMainGump::TMainGump()
:m_SavePassword(true), m_SelectedAccount(true), m_AccountPos(0), m_PasswordPos(0),
m_LastArrowTick(0), m_ArrowLighted(false)
{
	m_Account = "";
	m_Password = "";

	UO->m_Font->GenerateA(2, m_TextureLogin, "Log in to Ultima Online", 0x0386);

	UO->m_Font->GenerateA(2, m_TextureAccount, "Account Name", 0x0386);

	UO->m_Font->GenerateA(2, m_TexturePassword, "Password", 0x0386);

	UO->m_Font->GenerateA(9, m_TextureSavePassword, "Save Password", 0x0386);

	UO->m_Font->GenerateA(9, m_TextureAutoLogin, "Auto Login", 0x0386);
}
//---------------------------------------------------------------------------
TMainGump::~TMainGump()
{
	m_TextureLogin.Clear();
	m_TextureAccount.Clear();
	m_TexturePassword.Clear();
	m_TextureSavePassword.Clear();
	m_TextureAutoLogin.Clear();
}
//---------------------------------------------------------------------------
void TMainGump::InsertAccount(char *c)
{
	if (m_AccountPos < 0 || m_AccountPos > m_Account.length()) m_AccountPos = m_Account.length();
	m_Account.insert(m_AccountPos, c);
	m_AccountPos++;
}
//---------------------------------------------------------------------------
void TMainGump::InsertPassword(char *c)
{
	if (m_PasswordPos < 0 || m_PasswordPos > m_Password.length()) m_PasswordPos = m_Password.length();
	m_Password.insert(m_PasswordPos, c);
	m_PasswordPos++;
}
//---------------------------------------------------------------------------
void TMainGump::Init()
{
	g_ConfigLoaded = false;

	SetWindowTextA(g_hWnd, "Ultima Online");

	RECT r = {0, 0, 0, 0};
	r.right = 640;
	r.bottom = 480;
	AdjustWindowRectEx(&r, GetWindowLongA(g_hWnd, GWL_STYLE),FALSE, GetWindowLongA(g_hWnd, GWL_EXSTYLE));
	if (r.left < 0) r.right += (r.left * (-1));
	if (r.top < 0) r.bottom += (r.top * (-1));

	if (!m_SavePassword) m_Password = "";

	m_SelectedAccount = false;
	m_PasswordPos = m_Password.length();
	
	SetWindowPos(g_hWnd, HWND_TOP, 0, 0, r.right, r.bottom, 0);

	g_TotalSendSize = 0;
	g_TotalRecvSize = 0;

	UO->SetLightLevel(0);
	UO->SetPersonalLightLevel(0);

	UO->m_GL.SetLightLevel(0.0f);

	UO->m_Font->SetSavePixels(false);
}
//---------------------------------------------------------------------------
int TMainGump::Render(bool mode)
{
	DWORD ticks = GetTickCount();

	if (g_LastRenderTime > ticks)
	{
		if (mode || !g_SelectGumpObjects) return 0;
	}

	if (mode)
	{
		int CanSelectedButton = g_LastSelectedObject;

		int CanPressedButton = 0;
		if (g_LeftMouseDown && g_LastObjectLeftMouseDown == g_LastSelectedObject) CanPressedButton = g_LastObjectLeftMouseDown;
	
		g_LastRenderTime = ticks + g_FrameDelay;

		UO->m_GL.BeginDraw();
		
		UO->DrawGump(0x0588, 0, 0, 0, 640, 480); //Main Gump background
		UO->DrawGump(0x157C, 0, 0, 0); //Main Gump
		UO->DrawGump(0x15A0, 0, 0, 4); //Main Gump Notes
		UO->DrawResizepicGump(0x13BE, 128, 288, 451, 157); //ActPwd Container
		UO->DrawGump(0x058A, 0, 286, 45); //Main Gump Castle?

		WORD GumpID = 0x1589 + (int)(CanSelectedButton == 1); //X gump /lighted
		if (CanPressedButton == 1) GumpID = 0x158B; //X gump (pressed)
		UO->DrawGump(GumpID, 0, 555, 4);
		
		GumpID = 0x15A4 + (int)(m_ArrowLighted || (CanSelectedButton == 2)); //> gump
		if (CanPressedButton == 2) UO->DrawGump(0x15A6, 0, 610, 445); //> gump pressed
		UO->DrawGump(GumpID, 0, 610, 445);

		if (m_LastArrowTick < ticks)
		{
			m_ArrowLighted = !m_ArrowLighted;
			m_LastArrowTick = ticks + 500;
		}

		UO->DrawResizepicGump(0x0BB8, 328, 343, 210, 30); //Account text field
		UO->DrawResizepicGump(0x0BB8, 328, 383, 210, 30); //Password text field
		UO->DrawGump(0x00D3 - (int)(m_SavePassword == 0), 0, 328, 417); //Checkbox on / off
		UO->DrawGump(0x00D3 - (int)(g_AutoLogin == 0), 0, 183, 417); //Checkbox on / off
		
		m_TextureLogin.Draw(253, 305);
		m_TextureAccount.Draw(183, 345);
		m_TexturePassword.Draw(183, 385);
		m_TextureSavePassword.Draw(351, 421);
		m_TextureAutoLogin.Draw(206, 421);

		string cver("UO Version " + g_ClientVersionText + ".");
		UO->m_Font->DrawA(9, cver.c_str(), 0x034E, 286, 455);

		WORD TextColor = 0x034F;
		if (m_SelectedAccount) TextColor = 0x0021;
		else if (g_LastSelectedObject == 3) TextColor = 0x03E3;
		UO->m_Font->DrawA(5, m_Account.c_str(), TextColor, 335, 343);

		if (m_SelectedAccount)
		{
			int TextLen = UO->m_Font->GetWidthA(5, m_Account.c_str(), m_AccountPos);
			UO->m_Font->DrawA(5, "_", TextColor, 335 + TextLen, 345);
		}
		
		string str = UO->MaskText(m_Password.length());
		TextColor = 0x034F;
		if (!m_SelectedAccount) TextColor = 0x0021;
		else if (g_LastSelectedObject == 4) TextColor = 0x03E3;
		UO->m_Font->DrawA(5, str.c_str(), TextColor, 335, 385);

		if (!m_SelectedAccount)
		{
			int TextLen = UO->m_Font->GetWidthA(5, str.c_str(), m_PasswordPos);
			UO->m_Font->DrawA(5, "_", TextColor, 335 + TextLen, 387);
		}
		
		if (g_LastSelectedObject == 1) UO->SetTransparentHint(L"Quit Ultima Online", 80.0f);
		else if (g_LastSelectedObject == 2) UO->SetTransparentHint(L"Next screen");
		else if (g_LastSelectedObject == 3) UO->SetTransparentHint(L"Click here to enter your user name", 150.0f);
		else if (g_LastSelectedObject == 4) UO->SetTransparentHint(L"Click here to enter your password", 150.0f);
		else if (g_LastSelectedObject == 5) UO->SetTransparentHint(L"Save your password in config file", 150.0f);
		else if (g_LastSelectedObject == 6) UO->SetTransparentHint(L"Auto LogIn (without selection screens)", 150.0f);
		
		UO->DrawCursor(0x2073); //Main Gump mouse cursor

		UO->m_GL.EndDraw();
	}
	else
	{
		g_LastSelectedObject = 0;
		g_LastObjectType = SOT_NO_OBJECT;

		if (UO->GumpPixelsInXY(0x1589, 555, 4))
		{
			g_LastSelectedObject = 1; //X gump
		}
		else if (UO->GumpPixelsInXY(0x15A4, 610, 445))
		{
			g_LastSelectedObject = 2; //> gump
		}
		else if (UO->ResizepicPixelsInXY(0xBB8, 328, 343, 210, 30))
		{
			g_LastSelectedObject = 3; //Account text field
		}
		else if (UO->ResizepicPixelsInXY(0xBB8, 328, 383, 210, 30))
		{
			g_LastSelectedObject = 4; //Password text field
		}
		else if (UO->GumpPixelsInXY(0x00D3, 328, 417))
		{
			g_LastSelectedObject = 5; //Save password checkbox
		}
		else if (UO->GumpPixelsInXY(0x00D3, 183, 417))
		{
			g_LastSelectedObject = 6; //Auto Login checkbox
		}

		return g_LastSelectedObject;
	}
	return 0;
}
//---------------------------------------------------------------------------
void TMainGump::ParseLeftMouseDown()
{
	Render(false);

	g_DroppedLeftMouseX = g_MouseX;
	g_DroppedLeftMouseY = g_MouseY;
	g_LeftMouseDown = true;
	g_LastObjectLeftMouseDown = g_LastSelectedObject;

	if (g_LastSelectedObject == 0) return;

	if (g_LastSelectedObject == 3) //Account text field
	{
		m_SelectedAccount = true;
		m_AccountPos = m_Account.length();
	}
	else if (g_LastSelectedObject == 4) //Password text field
	{
		m_SelectedAccount = false;
		m_PasswordPos = m_Password.length();
	}
}
//---------------------------------------------------------------------------
void TMainGump::ParseLeftMouseUp()
{
	Render(false);

	g_LeftMouseDown = false;
	g_DroppedLeftMouseX = 0;
	g_DroppedLeftMouseY = 0;

	if (g_LastSelectedObject == 0 || g_LastSelectedObject != g_LastObjectLeftMouseDown)
	{
		g_LastObjectLeftMouseDown = 0;
		return;
	}

	if (g_LastObjectLeftMouseDown == 1) //x button
	{
		PostQuitMessage(0);
		if (UO != NULL) delete UO;
		UO = NULL;
		return;
	}
	else if (g_LastObjectLeftMouseDown == 2) //> button
	{
		UO->Connect();
	}
	else if (g_LastObjectLeftMouseDown == 5) //Main gump checkbox
	{
		m_SavePassword = !m_SavePassword;
	}
	else if (g_LastObjectLeftMouseDown == 6) //Auto Login checkbox
	{
		g_AutoLogin = !g_AutoLogin;
	}

	g_LastObjectLeftMouseDown = 0;
}
//---------------------------------------------------------------------------
void TMainGump::ParseRightMouseDown()
{
}
//---------------------------------------------------------------------------
void TMainGump::ParseRightMouseUp()
{
}
//---------------------------------------------------------------------------
bool TMainGump::ParseLeftMouseDoubleClick()
{
	return false;
}
//---------------------------------------------------------------------------
bool TMainGump::ParseRightMouseDoubleClick()
{
	return false;
}
//---------------------------------------------------------------------------
void TMainGump::ParseMouseWheel(MOUSE_WHEEL_STATE state)
{
}
//---------------------------------------------------------------------------
void TMainGump::ParseCharPress(WPARAM wparam, LPARAM lparam)
{
	if (m_FontIndex[wparam] == 0xFF) return;

	char ch[2] = {0};
	ch[0] = wparam;
	if (strlen((char*)ch) < 1) return;

	if (m_SelectedAccount && m_Account.length() < 16) //add char to account
	{
		InsertAccount(ch);
	}
	else if (!m_SelectedAccount && m_Password.length() < 16) //add char to password
	{
		InsertPassword(ch);
	}
}
//---------------------------------------------------------------------------
void TMainGump::ParseKeyPress(WPARAM wparam, LPARAM lparam)
{
	switch (wparam)
	{
		case VK_TAB:
		{
			if (m_SelectedAccount)
			{
				m_SelectedAccount = false;
				if (m_PasswordPos > m_Password.length()) m_PasswordPos = m_Password.length();
			}
			else
			{
				m_SelectedAccount = true;
				if (m_AccountPos > m_Account.length()) m_AccountPos = m_Account.length();
			}
			return;
		}
		case VK_RETURN:
		{
			UO->Connect();
			//UO->InitGump(GS_MAIN_CONNECT);
			return;
		}
		case VK_HOME:
		{
			if (m_SelectedAccount)
			{
				m_AccountPos = 0;
			}
			else
			{
				m_PasswordPos = 0;
			}
			return;
		}
		case VK_END:
		{
			if (m_SelectedAccount)
			{
				m_AccountPos = m_Account.length();
			}
			else
			{
				m_PasswordPos = m_Password.length();
			}
			return;
		}
		case VK_LEFT:
		{
			if (m_SelectedAccount && m_AccountPos > 0)
			{
				m_AccountPos--;
			}
			else if (!m_SelectedAccount && m_PasswordPos > 0)
			{
				m_PasswordPos--;
			}
			return;
		}
		case VK_RIGHT:
		{
			if (m_SelectedAccount && m_AccountPos < m_Account.length())
			{
				m_AccountPos++;
			}
			else if (!m_SelectedAccount && m_PasswordPos < m_Password.length())
			{
				m_PasswordPos++;
			}
			return;
		}
		case VK_BACK:
		{
			if (m_SelectedAccount && m_AccountPos > 0 && m_AccountPos <= m_Account.length())
			{
				if (m_AccountPos > 0 && m_AccountPos <= m_Account.length())
				{
					if (m_AccountPos == m_Account.length())
					{
						m_AccountPos--;
						m_Account.resize(m_AccountPos);
					}
					else
					{
						char str[30] = {0};
						strncpy(str, m_Account.c_str(), m_Account.length());
						memmove(str + m_AccountPos - 1, str + m_AccountPos, m_Account.length() - m_AccountPos);
						str[m_Account.length() - 1] = 0;
						m_Account = str;
						m_AccountPos--;
					}
				}
			}
			else if (!m_SelectedAccount && m_PasswordPos > 0 && m_PasswordPos <= m_Password.length())
			{
				if (m_PasswordPos > 0 && m_PasswordPos <= m_Password.length())
				{
					if (m_PasswordPos == m_Password.length())
					{
						m_PasswordPos--;
						m_Password.resize(m_PasswordPos);
					}
					else
					{
						char str[30] = {0};
						strncpy(str, m_Password.c_str(), m_Password.length());
						memmove(str + m_PasswordPos - 1, str + m_PasswordPos, m_Password.length() - m_PasswordPos);
						str[m_Password.length() - 1] = 0;
						m_Password = str;
						m_PasswordPos--;
					}
				}
			}
			return;
		}
		case VK_DELETE:
		{
			if (m_SelectedAccount && m_AccountPos < m_Account.length())
			{
				if (m_AccountPos < m_Account.length())
				{
					if (m_AccountPos == (m_Account.length() - 1))
					{
						m_Account.resize(m_AccountPos);
					}
					else
					{
						char str[30] = {0};
						strncpy(str, m_Account.c_str(), m_Account.length());
						memmove(str + m_AccountPos, str + m_AccountPos + 1, m_Account.length() - m_AccountPos - 1);
						str[m_Account.length() - 1] = 0;
						m_Account = str;
					}
				}
			}
			else if (!m_SelectedAccount && m_PasswordPos < m_Password.length())
			{
				if (m_PasswordPos < m_Password.length())
				{
					if (m_PasswordPos == (m_Password.length() - 1))
					{
						m_Password.resize(m_PasswordPos);
					}
					else
					{
						char str[30] = {0};
						strncpy(str, m_Password.c_str(), m_Password.length());
						memmove(str + m_PasswordPos, str + m_PasswordPos + 1, m_Password.length() - m_PasswordPos - 1);
						str[m_Password.length() - 1] = 0;
						m_Password = str;
					}
				}
			}
			return;
		}
		default: break;
	}
}
//---------------------------------------------------------------------------